<?php

declare(strict_types=1);
/**
 * This file is part of bearing-web.
 *
 * @link     https://www.bearing.cn/
 * @contact  tech@bearing.cn
 * @license  https://gitee.com/cppo/bearing_web/-/blob/master/README.md
 */

namespace common\models;

use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\BaseActiveRecord;

/**
 * This is the model class for table "{{%user_token}}".
 *
 * @property int $id
 * @property int $user_id
 * @property string $token
 * @property int $expire_time
 * @property int $create_time
 * @property int $update_time
 */
class UserTokenModel extends ActiveRecord
{
    public static function tableName(): string
    {
        return '{{%user_token}}';
    }

    public function rules(): array
    {
        return [
            [['create_time', 'update_time'], 'default', 'value' => 0],
            [['token'], 'required'],
            [['user_id', 'expire_time', 'create_time', 'update_time'], 'integer'],
            [['token'], 'string', 'max' => 32],
            [['user_id'], 'unique'],
            [['token'], 'unique'],
        ];
    }

    public function attributeLabels(): array
    {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'token' => 'Token',
            'expire_time' => 'Expire Time',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }

    /**
     * 时间更新策略.
     *
     * @return array[]
     */
    public function behaviors(): array
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    BaseActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],   // 创建
                    BaseActiveRecord::EVENT_BEFORE_UPDATE => ['update_time'],       // 修改
                ],
                'value' => function () {
                    return time();
                },
            ],
        ];
    }
}
